package com.flexycore.iphone.controller;



import com.flexycore.iphone.coregraphics.CGRect;
import com.flexycore.iphone.uikit.UIActionSheet;
import com.flexycore.iphone.uikit.UIActionSheetDelegate;
import com.flexycore.iphone.uikit.UIImage;
import com.flexycore.iphone.uikit.UIImageView;
import com.flexycore.iphone.uikit.UIView;
import com.flexycore.iphone.uikit.UIViewController;
import com.flexycore.iphone.view.MainView;

/**
 * 
 * @author matthieu nicolazo
 *
 */
public class ActionSheetDelegate extends UIActionSheetDelegate {
	/**
	 * Field
	 */
	private MainView view;
	
	/**
	 * 
	 * @param v		MainView that create this object.
	 */
	public ActionSheetDelegate(MainView v){
		super();
		init();
		view = v;
	}
	
	/**
	 * Method called by system when user select a button on action sheet.
	 * @param actionSheet		ActionSheet calling this method.
	 * @param buttonIndex		Index of selected button.
	 * 
	 */
	//@Override
	public void actionSheetClickedButtonAtIndex(UIActionSheet actionSheet,
			int buttonIndex) {
		if(buttonIndex == 0){
			// Click on "cancel" button
			return;
		}
		else {
			UIView newView = new UIView().init();
			
			// Add to the new UIView as many subviews as user wants.
			for(int i=1; i<=buttonIndex; i++){
				UIImageView imageView = new UIImageView();
				imageView.initWithFrame(CGRect.CGRectMake(i*40, 100, 30, 30));
				imageView.setImage(UIImage.imageNamed("img"+i+".tiff"));
				newView.addSubview(imageView);
			}
			
			UIViewController viewController = new UIViewController().init();
			if(buttonIndex == 1)
				viewController.setTitle(buttonIndex + " added item");
			else
				viewController.setTitle(buttonIndex + " added items");
			viewController.setView(newView);
			
			// Push this new view into UINavigationController stack.
			view.getNavigationController().pushViewControllerAnimated(viewController, false);
		}
	}
	
}
